function analogcorr(varargin)

global DFILE
global CONTROL
global ALLCH

sf=getmainselection;
vch = 1;
ich = 3;
if(nargin == 0)
    x=ALLCH{1};
    y=ALLCH{3};
else
    x = {varargin{1}};
    y = {varargin{2}};
end;

c={}; lags = {};
h = findobj('tag', 'simcorrfig');
if(isempty(h))
    h = figure;
    set(h, 'tag', 'simcorrfig');
else
    figure(h);
    clf;
end;
l = size(x,1);
c = [];
dt = DFILE.rate(1)/1000;
twin = DFILE;
maxlag = length(x(1,:));


ct = zeros(1, maxlag*2+1);
for i = 1:l
    xf = winfilt(detrend(x(i,:)), 10, 500, DFILE.rate(1));
    yf = winfilt(detrend(y(i,:)), 10, 500, DFILE.rate(1));
    [a, lags] = xcorr(xf, yf, maxlag, 'coeff');
    if(i == 1)
        c = a;
%        plot(xf);
%        hold on
%        plot(x(i,:), 'r-');
    else
        c = c + a;
    end;

end;

subplot('Position', [0.07, 0.1, 0.86, 0.35]);
c = c/size(x,1);
plot(lags*dt, c);
%stem(lags*dt, c, 'filled', 'linewidth', 2.0, 'markersize', 2.0, 'marker', 's');
%set(gca, 'Ylim', [-0.25 1]);
subplot('Position', [0.07, 0.50, 0.86, 0.35]);
plot(lags*dt, c);
set(gca, 'Xlim', [-100 100]);
[xm, jm] = max(c);
tjm = dt*lags(jm);

   subplot('Position',[0.07,0.89,0.86,0.10])
   axis([0,1,0,1])
   axis('off')
   text(0,0.8,sprintf('%-12s R[%d:%d] %-8s VCh: %d ICh: %d', ...
       DFILE.filename, DFILE.frec, DFILE.lrec, CONTROL(sf).protocol, vch, ich), 'Fontsize', 9);
   text(0,0.60,sprintf('Solution:%-12s  gain:%4.1f  LPF:%4.1f kHz', CONTROL(sf).solution, DFILE.igain, DFILE.low_pass(1)), 'FontSize', 9);
    text(0, 0.40, sprintf('Max Correl: %8.3f  at delay: %8.3f', c(jm), tjm));
    

